Skip to content

perf: 16 byte leaves for set 0 of the packed array - #74

Merged
alexvictoor merged 1 commit into
masterfrom
perf/packed-wide-leaves
Nov 22, 2026
Merged

alexvictoor merged 1 commit into
masterfrom
perf/packed-wide-leaves

Conversation

@alexvictoor

@alexvictoor alexvictoor commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Set 0 of the packed array (the low byte of every count, the dense one) gets 16-byte leaves. Sets 1..7 keep 8-byte leaves. Encoded bytes do not change.

 PackedArrayContext
-  LEAF_LEVEL_SHIFT = 3                 # 8-byte leaves for all sets
-  topLevelShift                        # one depth for all sets
+  LEAF_LEVEL_SHIFT_BY_SET = [4, 3, 3, 3, 3, 3, 3, 3]
+  LEAF_BYTE_MASK_BY_SET   = [15, 7, ...]
+  topLevelShiftBySet                   # set 0 is one level shallower
+  newLeafEntry(lengthInLongs)          # 2 longs for set 0, 1 for the others
   resize: copies whole leaves per set

 PackedArray.get / add / set
   descend set k with its own shift
-  byteInLeaf = packedIndex & 7
+  byteInLeaf = packedIndex & LEAF_BYTE_MASK_BY_SET[k]
   leafBase   = (packedIndex - byteInLeaf) >> 3

 leaf cache
+  set-0 entry kept across the two 8-index groups of a 16-index block,
+  so sequential scans walk set 0 once per block
flowchart LR
    I["virtual index"] --> S0["set 0: 16-byte leaf, shift 4"]
    I --> Sk["sets 1..7: 8-byte leaf, shift 3"]
    S0 --> D0["one level fewer of 16-slot entries"] --> L0["byte = leaf[index & 15]"]
    Sk --> Dk["same depth as before"] --> Lk["byte = leaf[index & 7]"]
Loading

Set 0 takes most accesses, and its descent is now one dependent memory read shorter. The leaf cache stores long indexes whatever the leaf width. PackedArray keeps a module-local copy of the mask table: reading it through the import binding made get 2.4x to 3.4x slower.

Evidence

  • Before: src/bench/packed-leaves.bench.ts on master 2b0e3ed.
    After: same bench on this branch. Three alternating runs each, iterations/s, 200k records per iteration.
Bench master branch
record, centre band (N=16384) 49.7 64.3 +29%
record, sparse tails (N=4096) 42.9 58.3 +36%
sequential get, sparse (N=4096) 12.8k 18.6k +45%
sequential get, centre (N=16384) 6.4k 8.0k +26%

32-byte set-0 leaves (tried in JS only) gave no further recording gain and a slower centre scan, so 16 is kept.

virtual length master branch
2^13 8192 (fell back) no fallback
2^14 7287 no fallback
2^15 3474 3535
2^16 3072 2573 (-16%)
2^17 2688 2192 (-18%)

Wide leaves win up to 2^15 and lose above (2^16, 2^17, and 40000 in #75's probe): a lone value costs 16 bytes in set 0 instead of 8, so the 64 KB packed limit is reached sooner. #75, stacked on this PR, keeps 16-byte leaves only up to 2^15 and converts the layout on resize across that threshold.

  • Before: no test pinned the leaf layout.
    After: src/packedarray/PackedArray.wideLeaves.spec.ts (8 tests: resize across the set-0 depth change, sharing within a 16-index block, no leaf for zero writes, sparse iteration, unpacked fallback, alternating groups, random checks against a plain array), assembly/__tests__/PackedArrayWideLeaves.test.ts (AS mirror), src/PackedHistogram.encoding.spec.ts (packed vs Int32: identical encoded base64 and percentiles).
npm test            281 passed
npm run astest:ci   146509 / 146509
npm run typecheck   clean

Merge Danger

Door: two-way

Internal layout only; a revert restores 8-byte leaves everywhere. Encoding is unaffected, which the packed-vs-Int32 test checks.

Blast Radius: packed

Every packed-array access and resize, in JS and AS.

🤖 Generated with Claude Code

@alexvictoor
alexvictoor force-pushed the perf/packed-wide-leaves branch from 4cbe522 to 06d6703 Compare November 15, 2026 21:17
Set 0 (the dense low byte) gets 16 byte leaves, so its set tree is one
level shallower for some virtual lengths. Sets 1..7 keep 8 byte leaves.
Each set has its own leaf shift and top level shift; the leaf cache
stores long indexes as before. Encoded bytes do not change.

The leaf cache keeps set 0's entry across the two 8 index groups of a
16 index block, so sequential scans walk set 0 once per block.

Measured with src/bench/packed-leaves.bench.ts, master vs branch, three
alternating runs each (hz = iterations per second, 200k records each):
- record centre band:   ~49.7 -> ~64.3 (about +29%)
- record sparse tails:  ~42.9 -> ~58.3 (about +36%)
- sequential get, sparse (N=4096):  ~12.8k -> ~18.6k (about +45%)
- sequential get, centre (N=16384): ~6.4k  -> ~8.0k  (about +26%)

Memory cost: isolated values reach the unpacked fallback later at
virtual lengths 2^13..2^15 and sooner at 2^16 and 2^17 (about 16-18%
fewer distinct values before fallback).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@alexvictoor
alexvictoor force-pushed the perf/packed-wide-leaves branch from 06d6703 to 692bfe4 Compare November 22, 2026 21:26
@alexvictoor
alexvictoor marked this pull request as ready for review November 22, 2026 21:34
@alexvictoor
alexvictoor merged commit e466327 into master Nov 22, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant